package com.oanda.blackberry.ui;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.EditField;

import com.oanda.blackberry.restapi.Quote;
import com.oanda.blackberry.restapi.Symbol;
import com.oanda.blackberry.util.Model;
import com.oanda.blackberry.util.StringUtils;

public class TradeLimitEditField extends EditField implements TradeTypeChangeListener,
        QuoteChangeListener {
    private Bitmap tp = Bitmap.getBitmapResource("tp-label-72x35.png");
    private Bitmap selected = Bitmap.getBitmapResource("symbol-selected-18x18-p24.png");
    private Bitmap notSelected = Bitmap.getBitmapResource("symbol-notselected-18x18.png");
    int fontColor;
    int titleColor = -1;
    String title;
    boolean isActivated = false;

    int limitType = 0;
    public static final int TYPE_TP = 0;
    public static final int TYPE_SL = 1;
    public static final int TYPE_TS = 2;
    public static final int TYPE_UB = 3;
    public static final int TYPE_LB = 4;
    boolean isEdited = false;
    ActiveListener a;
    Quote quote;
    String type = null;
    String lastInitialPrice;

    public TradeLimitEditField(int color) {
        this.fontColor = color;
    }

    public TradeLimitEditField(int color, String label) {
        super("", label);
        this.fontColor = color;
    }

    public TradeLimitEditField(int color, String title, String label) {
        super(title, label);
        this.fontColor = color;
    }

    public TradeLimitEditField(int color, String title, String text, long style) {
        super(title, text, 50, style);
        this.fontColor = color;
    }

    public TradeLimitEditField(int labelColor, int color, String title, String label, long style) {
        super(title, label, 50, style);
        this.fontColor = color;
    }

    public TradeLimitEditField(int color, String title, String text, int maxSize, long style) {
        super(title, text, maxSize, style);
        this.fontColor = color;
    }

    String getTextWithSameWidth(String s) {
        String t = "";
        int w = getFont().getAdvance(s);
        for (int i = 0; (s != null) && (i < w);) {
            t = t + " ";
            i = getFont().getAdvance(t);
        }
        return t;
    }

    public TradeLimitEditField(int titleColor, int color, String title, String text, int maxSize,
            long style) {
        super("", text, maxSize, style);
        String t = getTextWithSameWidth(title);
        setLabel(t);

        this.title = title;
        this.fontColor = color;
        this.titleColor = titleColor;
    }

    public void setTitleColor(int titleColor) {
        this.titleColor = titleColor;
    }

    public void setFontColor(int fontColor) {
        this.fontColor = fontColor;
    }

    public void paint(Graphics g) {
        if (this.titleColor == -1) {
            this.titleColor = this.fontColor;
        }

        g.setColor(this.titleColor);
        g.setFont(getFont());

        if (!this.isActivated) {
            switch (this.limitType) {
            case 1:
                g.drawText("SL Price", 8, 0);
                break;
            case 2:
                g.drawText("TS Pips", 8, 0);
                break;
            case 3:
                g.drawText("UB Price", 8, 0);
                break;
            case 4:
                g.drawText("LB Price", 8, 0);
                break;
            case 0:
                g.drawText("TP Price", 8, 0);
            }

            setText("");
            return;
        }

        g.setColor(this.fontColor);
        super.paint(g);
    }

    public void setText(String text) throws IllegalArgumentException {
        try {
            if (text == null)
                super.setText("---");
            else if (text.length() == 0)
                super.setText("---");
            else
                super.setText(text);
        } catch (IllegalArgumentException ia) {
            super.setText("");
        }
    }

    protected void makeContextMenu(ContextMenu contextMenu) {
    }

    protected void onUnfocus() {
        if ((getText().length() == 0) && (this.isActivated)) {
            changeSelectedSymbol();
        }
    }

    protected boolean keyDown(int keycode, int time) {
        if (Keypad.map(keycode) == '0')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '1')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '2')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '3')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '4')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '5')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '6')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '7')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '8')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '9')
            this.isEdited = true;
        else if (Keypad.map(keycode) == '0')
            this.isEdited = true;
        else if (Keypad.key(keycode) == 8)
            this.isEdited = true;

        if (!this.isActivated) {
            changeSelectedSymbol();
        } else if (Keypad.key(keycode) == 8) {
            this.isEdited = false;
            if (getText().length() == 1) {
                changeSelectedSymbol();
                setText("dsafa");
                invalidate();
            }

        }

        return super.keyDown(keycode, time);
    }

    protected boolean trackwheelClick(int status, int time) {
        changeSelectedSymbol();

        return true;
    }

    private void changeSelectedSymbol() {
        this.isActivated = (!this.isActivated);

        if (((!this.isActivated) || (this.isEdited) || (getText().length() < 1)
                || (this.lastInitialPrice == null) || (!getText().equals(this.lastInitialPrice)))
                || (this.a != null)) {
            this.a.change();
        }
        invalidate();
    }

    public void setActiveListener(ActiveListener a) {
        this.a = a;
    }

    public boolean isActive() {
        return this.isActivated;
    }

    public void setQuote(Quote quote) {
        if (this.quote == null) {
            this.isEdited = false;
        }
        boolean quoteChanged = false;

        if ((this.quote != null) && (!this.quote.getSymbol().equals(quote.getSymbol()))) {
            this.isEdited = false;
            quoteChanged = true;
        }

        this.quote = quote;
        if ((this.isActivated)
                && (!this.isEdited)
                && ((getText().length() < 1)
                        || ((this.lastInitialPrice != null) && (getText()
                                .equals(this.lastInitialPrice))) || (quoteChanged))) {
            quoteChanged = false;
        }
    }

    public void setTradeType(String type) {
        this.type = type;
        if (this.quote == null)
            this.isEdited = false;
        if ((this.quote != null) && (!this.quote.getSymbol().equals(this.quote.getSymbol()))) {
            this.isEdited = false;
        }
        if ((this.isActivated) && (!this.isEdited) && (getText().length() >= 1)
                && (this.lastInitialPrice != null) && (getText().equals(this.lastInitialPrice)))
            ;
    }

    private String getInitialPrice() {
        if (this.quote == null)
            return "";
        if (this.type == null)
            return "";
        Symbol s = Model.getModel().getSettings().getRate().getSymbol(this.quote.getSymbol());
        if (s == null)
            return "";

        switch (this.limitType) {
        case 4:
            if (this.type.equals("buy")) {
                double price = this.quote.getAsk() - 10.0D * s.getPiploc();
                this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
                return this.lastInitialPrice;
            }
            double price = this.quote.getBid() + 10.0D * s.getPiploc();
            this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
            return this.lastInitialPrice;
        case 3:
            if (this.type.equals("buy")) {
                price = this.quote.getAsk() + 10.0D * s.getPiploc();
                this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
                return this.lastInitialPrice;
            }
            price = this.quote.getBid() - 10.0D * s.getPiploc();
            this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
            return this.lastInitialPrice;
        case 0:
            if (this.type.equals("buy")) {
                price = this.quote.getBid() + 10.0D * s.getPiploc();
                this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
                return this.lastInitialPrice;
            }
            price = this.quote.getAsk() - 10.0D * s.getPiploc();
            this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
            return this.lastInitialPrice;
        case 1:
            if (this.type.equals("buy")) {
                price = this.quote.getBid() - 10.0D * s.getPiploc();
                this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
                return this.lastInitialPrice;
            }
            price = this.quote.getAsk() + 10.0D * s.getPiploc();
            this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
            return this.lastInitialPrice;
        case 2:
            if (this.type.equals("buy")) {
                price = this.quote.getBid() - 10.0D * s.getPiploc();
                this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
                return this.lastInitialPrice;
            }
            price = this.quote.getAsk() + 10.0D * s.getPiploc();
            this.lastInitialPrice = StringUtils.formatDouble(price, s.getPrecision());
            return this.lastInitialPrice;
        }

        return "";
    }

    public void setTradeLimitType(int type) {
        this.limitType = type;
    }
}
